package com.dollar.juc.current_limit.semphore;

import java.util.Random;

public class Test {
    public static void main(String[] args) {
        // 创建对象池
        ObjPool<Long, String> pool = new ObjPool<Long, String>(10, 2L);
        for (int i = 0; i < 100; i++) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    // 通过对象池获取t，之后执行
                    try {
                        pool.exec(t -> {
                            try {
                                Thread.sleep(new Random(1211L).nextInt(10000));
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                            System.out.println(Thread.currentThread().getName() + ":::" + t);
                            return t.toString();
                        });
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }, "thraed-" + i).start();
        }

    }
}
